package com.example.demo.roads.service;

import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.roads.mapper.RoadsMapper;
import com.example.demo.roads.ov.res.PoiRes;
import com.example.demo.roads.ov.res.RoadsRes;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 道理数据算法
 *
 * @author yuanzheng
 * @since 2024-09-24
 */
@Component
@DS("postgresql")
public class RoadsComponent {

    @Resource
    private RoadsMapper roadsMapper;

    public String queryCrossing() {
        // 1.获取所有线路数据
        List<RoadsRes> roadsList = roadsMapper.queryRoads();
        roadsList.forEach(roadsRes -> {
            // 2.获取交叉口
            List<PoiRes> crossList = roadsMapper.queryCrossing(roadsRes);
            // 3. 数据入库
            crossList.forEach(poiRes -> {
                poiRes.setId(IdUtil.simpleUUID());
                poiRes.setType("cross");
                roadsMapper.insertPoi(poiRes);
            });
        });
        return "OK";
    }

    public String queryRoadSegment() {
        // 1.获取所有路名称
        List<RoadsRes> roadsList = roadsMapper.queryRoadsName();
        roadsList.forEach(roadsRes -> {
            // 2.获取交叉口
            List<PoiRes> poiList = roadsMapper.queryRoadsPoi(roadsRes);
            String roadName = roadsRes.getName();
            // 3. 数据入库
            for (int i = 0; i < poiList.size(); i++) {
                if (i + 1 == poiList.size()) {
                    break;
                }
                PoiRes poiA = poiList.get(i);
                PoiRes poiB = poiList.get(i + 1);
                String roadNameA = poiA.getName().replace(roadName, "").replace("交叉口", "");
                String roadNameB = poiB.getName().replace(roadName, "").replace("交叉口", "");
                if (!roadNameA.equals(roadNameB)) {
                    // 西段
                    PoiRes x = new PoiRes();
                    x.setId(IdUtil.simpleUUID());
                    x.setType("road_segment");
                    x.setLongitude(poiA.getLongitude());
                    x.setLatitude(poiA.getLatitude());
                    x.setGeometry(poiA.getGeometry());
                    x.setName(roadName + roadNameA + roadNameB + "西段");
                    roadsMapper.insertPoi(x);
                    // 东段
                    PoiRes d = new PoiRes();
                    d.setId(IdUtil.simpleUUID());
                    d.setType("road_segment");
                    d.setLongitude(poiB.getLongitude());
                    d.setLatitude(poiB.getLatitude());
                    d.setGeometry(poiB.getGeometry());
                    d.setName(roadName + roadNameA + roadNameB + "东段");
                    roadsMapper.insertPoi(d);
                    // 中段
                    PoiRes poi = roadsMapper.queryMiddlePoint(poiA.getGeometry(), poiB.getGeometry());
                    poi.setId(IdUtil.simpleUUID());
                    poi.setType("road_segment");
                    poi.setName(roadName + roadNameA + roadNameB + "中段");
                    roadsMapper.insertPoi(poi);
                }
            }
        });
        return "OK";
    }

    public String queryStreetSegment() {
        // 1.获取所有路名称
        List<RoadsRes> streetList = roadsMapper.queryStreetsName();
        streetList.forEach(streetsRes -> {
            // 2.获取交叉口
            List<PoiRes> poiList = roadsMapper.queryStreetsPoi(streetsRes);
            String roadName = streetsRes.getName();
            // 3. 数据入库
            for (int i = 0; i < poiList.size(); i++) {
                if (i + 1 == poiList.size()) {
                    break;
                }
                PoiRes poiA = poiList.get(i);
                PoiRes poiB = poiList.get(i + 1);
                String roadNameA = poiA.getName().replace(roadName, "").replace("交叉口", "");
                String roadNameB = poiB.getName().replace(roadName, "").replace("交叉口", "");
                if (!roadNameA.equals(roadNameB)) {
                    // 南段
                    PoiRes n = new PoiRes();
                    n.setId(IdUtil.simpleUUID());
                    n.setType("street_segment");
                    n.setLongitude(poiA.getLongitude());
                    n.setLatitude(poiA.getLatitude());
                    n.setGeometry(poiA.getGeometry());
                    n.setName(roadName + roadNameA + roadNameB + "南段");
                    roadsMapper.insertPoi(n);
                    // 北段
                    PoiRes b = new PoiRes();
                    b.setId(IdUtil.simpleUUID());
                    b.setType("street_segment");
                    b.setLongitude(poiA.getLongitude());
                    b.setLatitude(poiA.getLatitude());
                    b.setGeometry(poiA.getGeometry());
                    b.setName(roadName + roadNameA + roadNameB + "北段");
                    roadsMapper.insertPoi(b);
                    // 中段
                    PoiRes poi = roadsMapper.queryMiddlePoint(poiA.getGeometry(), poiB.getGeometry());
                    poi.setId(IdUtil.simpleUUID());
                    poi.setType("street_segment");
                    poi.setName(roadName + roadNameA + roadNameB + "中段");
                    roadsMapper.insertPoi(poi);
                }
            }
        });
        return "OK";
    }

    public String queryRoadSegmentNew() {
        // 1.获取所有路名称
        List<RoadsRes> roadsList = roadsMapper.queryRoadsName();
        roadsList.forEach(roadsRes -> {
            // 2.获取交叉口
            List<PoiRes> poiList = roadsMapper.queryRoadsPoi(roadsRes);
            String roadName = roadsRes.getName();
            // 3. 数据入库
            for (int i = 0; i < poiList.size(); i++) {
                if (i + 1 == poiList.size()) {
                    break;
                }
                PoiRes poiA = poiList.get(i);
                PoiRes poiB = poiList.get(i + 1);
                double distance = roadsMapper.queryDistance(poiA.getGeometry(), poiB.getGeometry());
                if (distance < 0.001) {
                    continue;
                }
                String roadNameA = poiA.getName().replace(roadName, "").replace("交叉口", "");
                String roadNameB = poiB.getName().replace(roadName, "").replace("交叉口", "");
                if (!roadNameA.equals(roadNameB)) {
                    // 中段
                    PoiRes poi = roadsMapper.queryMiddlePoint(poiA.getGeometry(), poiB.getGeometry());
                    poi.setId(IdUtil.simpleUUID());
                    poi.setType("road_segment");
                    poi.setName(roadName + roadNameA + roadNameB + "中段");
                    roadsMapper.insertPoi(poi);
                    // 西段
                    PoiRes x = roadsMapper.queryMiddlePoint(poiA.getGeometry(), poi.getGeometry());
                    x.setId(IdUtil.simpleUUID());
                    x.setType("road_segment");
                    x.setName(roadName + roadNameA + roadNameB + "西段");
                    roadsMapper.insertPoi(x);
                    // 东段
                    PoiRes d = roadsMapper.queryMiddlePoint(poi.getGeometry(), poiB.getGeometry());
                    d.setId(IdUtil.simpleUUID());
                    d.setType("road_segment");
                    d.setName(roadName + roadNameA + roadNameB + "东段");
                    roadsMapper.insertPoi(d);

                }
            }
        });
        return "OK";

    }

    public String queryStreetSegmentNew() {
        // 1.获取所有路名称
        List<RoadsRes> streetList = roadsMapper.queryStreetsName();
        streetList.forEach(streetsRes -> {
            // 2.获取交叉口
            List<PoiRes> poiList = roadsMapper.queryStreetsPoi(streetsRes);
            String roadName = streetsRes.getName();
            // 3. 数据入库
            for (int i = 0; i < poiList.size(); i++) {
                if (i + 1 == poiList.size()) {
                    break;
                }
                PoiRes poiA = poiList.get(i);
                PoiRes poiB = poiList.get(i + 1);
                double distance = roadsMapper.queryDistance(poiA.getGeometry(), poiB.getGeometry());
                if (distance < 0.001) {
                    continue;
                }
                String roadNameA = poiA.getName().replace(roadName, "").replace("交叉口", "");
                String roadNameB = poiB.getName().replace(roadName, "").replace("交叉口", "");
                if (!roadNameA.equals(roadNameB)) {
                    // 中段
                    PoiRes poi = roadsMapper.queryMiddlePoint(poiA.getGeometry(), poiB.getGeometry());
                    poi.setId(IdUtil.simpleUUID());
                    poi.setType("street_segment");
                    poi.setName(roadName + roadNameA + roadNameB + "中段");
                    roadsMapper.insertPoi(poi);
                    // 南段
                    PoiRes n = roadsMapper.queryMiddlePoint(poiA.getGeometry(), poi.getGeometry());
                    n.setId(IdUtil.simpleUUID());
                    n.setType("street_segment");
                    n.setName(roadName + roadNameA + roadNameB + "南段");
                    roadsMapper.insertPoi(n);
                    // 北段
                    PoiRes b = roadsMapper.queryMiddlePoint(poi.getGeometry(), poiB.getGeometry());
                    b.setId(IdUtil.simpleUUID());
                    b.setType("street_segment");
                    b.setName(roadName + roadNameA + roadNameB + "北段");
                    roadsMapper.insertPoi(b);
                }
            }
        });
        return "OK";
    }
}
